package com.zyh.system.supermark1.service.impl;

import com.zyh.system.supermark1.dao.impl.BuyCarDaoImpl;
import com.zyh.system.supermark1.domain.BuyCar;
import com.zyh.system.supermark1.domain.User;
import com.zyh.system.supermark1.service.BuyCarService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.Scanner;

public class BuyCarServiceImpl implements BuyCarService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final BuyCarDaoImpl carDaoImpl = new BuyCarDaoImpl();

    @Override
    public void add(BuyCar buyCar) {
        carDaoImpl.add(buyCar);
    }

    @Override
    public void showBuyCar(LinkedList<BuyCar> buyCars) {
        //总额：
        BigDecimal dmoney = new BigDecimal("0.0");
        double money = 0;
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t购买数量");
        for (BuyCar buyCar : buyCars) {
            System.out.println(buyCar.getId() + "\t\t" + buyCar.getName() + "\t\t" + buyCar.getPrice() + "\t\t" + buyCar.getBuyNum());
            BigDecimal buyNum = new BigDecimal(buyCar.getBuyNum());
            dmoney = dmoney.add((buyCar.getPrice().multiply(buyNum)));
            dmoney.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN);
            money = dmoney.doubleValue();
        }
        System.out.println("总金额为：" + money);
        if (buyCars.size() == 0) {
            return;
        }
        System.out.println("您是否付款？(是y 否任意键)");
        String confirm = INPUT.next();
        switch (confirm) {
            case "y":
                while (true) {
                    System.out.println("请输入付款金额：");
                    int payMoney = INPUT.nextInt();
                    if (payMoney < money) {
                        System.out.println("金额不足");
                        continue;
                    } else {
                        BigDecimal b1 = new BigDecimal((payMoney - money));
                        BigDecimal zhaoLing = b1.divide(new BigDecimal("1"), 1, BigDecimal.ROUND_DOWN);
                        System.out.println("找零：" + zhaoLing);
                        carDaoImpl.clearBuyCar(buyCars);
                        return;
                    }
                }
            default:
                return;
        }
    }

    //展示信息
    @Override
    public void showUserInfo(User user) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = sdf.format(date);
        System.out.println("\t\t\t\t\t\t\t当前时间为："+format);
        carDaoImpl.showUserInfo(user);
    }
}
